#####
## Replication File:
##
## Why Me? The Role of Victim 
## Identity in American Politics 
##
## By: Miles T. Armaly
## & Adam M. Enders
#####

library(lattice)
library(latticeExtra)
library(MASS)
library(foreign)
library(gridExtra)
library(car)

################################################################################

# Set working directory
vicdata <- read.dta("Clean Data.dta")
head(vicdata)

################################################################################

#####
## Figure 1 (6X6)
#####

vicdata$edufac <- NA
vicdata$edufac[which(vicdata$v11 >= 4)] <- "HS or Less"
vicdata$edufac[which(vicdata$v11 < 4)] <- "BA or More"

vicdata$genfac <- NA
vicdata$genfac[which(vicdata$female == 1)] <- "Female"
vicdata$genfac[which(vicdata$female == 0)] <- "Male"

vicdata$whitefac <- NA
vicdata$whitefac[which(vicdata$white == 1)] <- "White"
vicdata$whitefac[which(vicdata$white == 0)] <- "Non-White"

densityplot(~systemic2 | edufac * whitefac,
            data = vicdata,
            xlab = "Systemic Victimhood",
            groups = vicdata$genfac,
            plot.points = FALSE,
            col = c("black"),
            lty = c(1, 2),
            key=list(columns=1,lines=list(lty=c(1, 2), lwd=2), 
                     text=list(c("Female", "Male"))),
)

densityplot(~egocentric2 | edufac * whitefac,
            data = vicdata,
            xlab = "Egocentric Victimhood",
            groups = vicdata$genfac,
            plot.points = FALSE,
            col = c("black"),
            lty = c(1, 2),
            key=list(columns=1,lines=list(lty=c(1, 2), lwd=2), 
                     text=list(c("Female", "Male"))),
)

################################################################################

#####
## Figure 2
#####

vic <- read.csv("victimizers.csv")

vic$reg <- reorder(vic$reg, vic$order)
xyplot(as.factor(reg) ~ e_est, 
       data = vic,
       aspect = 1.5, 
       xlab = "OLS Estimate",
       ylab = "",
       xlim = c(-.6,2.6),
       col = "black",
       pch = 16,
       key=list(columns=1, points=list(pch=c(16, 17)),
                text=list(c("Egocentric", "Systemic"))),
       panel = function(...) {
               panel.xyplot(..., lty = 2)
               panel.abline(v=0, lty = 2)
               panel.dotplot(vic$s_est, as.factor(vic$reg), col = "black", pch = 17)
               panel.segments(vic$e_lb, as.factor(vic$reg),
                              vic$e_ub, as.factor(vic$reg), col = "black")
               panel.segments(vic$s_lb, as.factor(vic$reg),
                              vic$s_ub, as.factor(vic$reg), col = "black")
       }
)

################################################################################

#####
## Figure 3 (6.5X4.5)
#####

# Ideology
stacked1 <- stack(as.data.frame(cbind(vicdata$systemic2, vicdata$egocentric2)))
colnames(stacked1) <- c("victim", "type")
stacked1$victype[which(stacked1$type == "V1")] <- "Systemic"
stacked1$victype[which(stacked1$type == "V2")] <- "Egocentric"
stacked2 <- stack(as.data.frame(cbind(vicdata$ideo, vicdata$ideo)))
colnames(stacked2) <- c("ideo", "id")

ideodata <- as.data.frame(cbind(stacked1, stacked2))

ideodata$cond[ideodata$victype=="Egocentric"] <- 1
ideodata$cond[ideodata$victype=="Systemic"] <- 2

set.seed(1234)
xyplot(jitter(victim) ~ jitter(ideo) | as.factor(victype),
       data = ideodata,
       aspect = 1,
       main = "B. Ideological Self-Identification",
       xlab = " ",
       ylab = "Victimhood Scale",
       scond = ideodata$cond,
       scales=list(
               x=list(labels=c("Extreme\nLiberal", "", "", "Moderate", "", "", "Extreme\nConserv."), 
                      at=c(-3, -2, -1, 0, 1, 2, 3))),
       panel = function(x, y, col, scond, ...){
               pnl = panel.number()
               panel.xyplot(jitter(x), jitter(y), col = "light gray")
               panel.smoother(ideodata$ideo[scond==pnl], ideodata$victim[scond==pnl], col = "black", span=0.7)
       }
)


# Partisanship
stacked3 <- stack(as.data.frame(cbind(vicdata$systemic2, vicdata$egocentric2)))
colnames(stacked3) <- c("victim", "type")
stacked3$victype[which(stacked3$type == "V1")] <- "Systemic"
stacked3$victype[which(stacked3$type == "V2")] <- "Egocentric"
stacked4 <- stack(as.data.frame(cbind(vicdata$pid, vicdata$pid)))
colnames(stacked4) <- c("pid", "id")

piddata <- as.data.frame(cbind(stacked3, stacked4))

piddata$cond[piddata$victype=="Egocentric"] <- 1
piddata$cond[piddata$victype=="Systemic"] <- 2

set.seed(1234)
xyplot(jitter(victim) ~ jitter(pid) | as.factor(victype),
       data = piddata,
       aspect = 1,
       main = "A. Partisanship",
       xlab = " ",
       ylab = "Victimhood Scale",
       scond = piddata$cond,
       scales=list(
               x=list(labels=c("Strong\nDem.", "", "", "Indep.", "", "", "Strong\nRep."), 
                      at=c(-3, -2, -1, 0, 1, 2, 3))),
       panel = function(x, y, col, scond, ...){
               pnl = panel.number()
               panel.xyplot(jitter(x), jitter(y), col = "light gray")
               panel.smoother(piddata$pid[scond==pnl], piddata$victim[scond==pnl], col = "black")
       }
)

################################################################################

#####
## Figure 4 (6.5X4.5)
#####

vote <- read.csv("Trump Vote.csv")

xyplot(pred + lower + upper ~ vid | as.factor(type),
       data = vote,
       type = "l",
       lty = c(1,2,2),
       col = "black",
       ylab = "Pr(Trump Vote Choice)",
       aspect = 1,
       xlab = "Victimhood Scale"
)

therm <- read.csv("Trump Therm.csv")

xyplot(pred + lower + upper ~ vid | as.factor(type),
       data = therm,
       type = "l",
       lty = c(1,2,2),
       col = "black",
       ylab = "Trump Thermometer",
       aspect = 1,
       xlab = "Victimhood Scale"
)

################################################################################

####
## Figure 5 (6X7)
####

# Systemic
pred1 <- read.csv("Systemic, Racial Attitudes.csv", header=TRUE)

plot1 <- xyplot(estimate ~ scale | item, 
                data = pred1,
                xlab = "Victimhood Scale",
                ylab = "Predicted Attitudes",
                aspect = 1,
                #index.cond=list(c(4, 3, 1, 2)),
                groups = group,
                ylim = c(0.2, 0.8),
                col = "red3",
                alpha = c(1, 0.5, 0.5),
                type = "l",  
                lty = c(1, 2, 2),
                key=list(columns=1, lines=list(lty=c(1, 1), col=c("red3", "forestgreen")),
                         text=list(c("Systemic", 
                                     "Egocentric"))),
)

# Egocentric
pred2 <- read.csv("Egocentric, Racial Attitudes.csv", header=TRUE)

plot2 <- xyplot(estimate ~ scale | item, 
                data = pred2,
                xlab = " ",
                aspect = 1,
                ylim = c(0.2, 0.8),
                groups = group,
                col = "forestgreen",
                alpha = c(1, 0.5, 0.5),
                type = "l",  
                lty = c(1, 2, 2),
)

plot1 + plot2

################################################################################

####
## Figure 6
####

open <- read.csv("openended.csv")

barchart(percent*100~as.factor(issue),
         data = open,
         horizontal = FALSE,
         ylab = "Percent of Total",
         xlab = "Primary Issue Mentioned in Open-ended Response",
         scales=list(x=list(rot = 45)),
         ylim = c(-.75, 26)
)

################################################################################

#####
## Appendix: Figure A1
#####

histogram(~as.factor(q3_1-17),
          data = vicdata,
          xlab = "The system works against people like me"
)

histogram(~as.factor(q3_2-17),
          data = vicdata,
          xlab = "The world is `doing it to me'..."
)

histogram(~as.factor(q3_3-17),
          data = vicdata,
          xlab = "The world is out to get me"
)

histogram(~as.factor(q3_5-17),
          data = vicdata,
          xlab = "The system is rigged to benefit a select few"
)

################################################################################

#####
## Appendix: Figure A2
#####

histogram(~as.factor(q2_1 - 14),
          data = vicdata,
          xlab = "I rarely get what I deserve in life"
)

histogram(~as.factor(q2_2 - 14),
          data = vicdata,
          xlab = "Great things never come to me"
)

histogram(~as.factor(q2_3 - 14),
          data = vicdata,
          xlab = "I usually have to settle for less"
)

histogram(~as.factor(q2_5 - 14),
          data = vicdata,
          xlab = "I never seem to get an extra break"
)

################################################################################

#####
## Appendix: Figure A3
#####

histogram(~systemic2,
          data = vicdata,
          aspect = 1,
          xlab = "Systemic Victimhood"
)

histogram(~egocentric2,
          data = vicdata,
          aspect = 1,
          xlab = "Egocentric Victimhood"
)

cor(vicdata$systemic, vicdata$egocentric)
set.seed(1234)
xyplot(jitter(systemic2) ~ jitter(egocentric2),
       data = vicdata,
       aspect = 1,
       ylab = "Systemic Victimhood",
       xlab = "Egocentric Victimhood",
       panel = function(x, y, ...){
         panel.xyplot(x, y, col = "dark grey")
         panel.loess(vicdata$egocentric2, vicdata$systemic2, 
                     col = "black", lwd = 2)
         panel.text(0.08, 0.9, "r = 0.56")
       }
)


